﻿using Newtonsoft.Json.Linq;
using StorageApp.APIS;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace StorageApp
{
    /// <summary>
    /// _operator.xaml 的交互逻辑
    /// </summary>
    public partial class opencard : Window
    { 
        public opencard()
        { 
            initUserAsync();
            InitializeComponent();
        } 
        public string open_card = "";

        string paged = "1";
        string pagesize = "40";
        string filterdata = "&filter[0][is_scrap][type]=eq&filter[0][is_scrap][value]=0";
        public   void initUserAsync()
        {  
                setOpenCard(); 
        }
        public async void setOpenCard()
        {
            var res = await api.getWip(filterdata, paged, pagesize);
            details.ItemsSource = res["data"]["rows"];
            pageSize.Text=((int)(int.Parse(res["data"]["total"].ToString())/ int.Parse(pagesize))).ToString();
            page.Text = res["data"]["page"].ToString();
        }

        void OnLostFocus(object sender, RoutedEventArgs e)
        {
            System.Windows.Controls.TextBox tb = e.Source as System.Windows.Controls.TextBox;
            tb.PreviewMouseDown += new MouseButtonEventHandler(OnPreviewMouseDown);
        }

        void OnPreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            System.Windows.Controls.TextBox tb = e.Source as System.Windows.Controls.TextBox;
            tb.Focus();
            e.Handled = true;
        }
        //下一页
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            paged=(int.Parse(paged)+1).ToString();
            setOpenCard();
        }
        //上一页
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            paged = (int.Parse(paged) - 1).ToString();
            if (paged == "0")
            {
                paged = "1";
            }
            setOpenCard();
        }
        //查询
        private void ok_Click(object sender, RoutedEventArgs e)
        {
            
                filterdata = "&filter[0][is_scrap][type]=eq&filter[0][is_scrap][value]=0";
            if (textOpen_card.Text.Trim() != "")
            {
                filterdata += "&filter[1][open_card.code][type]=lk&filter[1][open_card.code][value]=" + textOpen_card.Text.Trim();
            }
            if (txtCustomer.Text.Trim() != "")
            {
                filterdata = "&filter[2][company.name][type]=lk&filter[2][company.name][value]=" + txtCustomer.Text.Trim();
            }
            if (txtDyelot.Text.Trim() != "")
            {
                filterdata = "&filter[3][dyelot][type]=lk&filter[3][dyelot][value]=" + txtDyelot.Text.Trim();
            }
            paged = "1";
            setOpenCard();
        }

        private void details_MouseUp(object sender, MouseButtonEventArgs e)
        { 
                var data = details.SelectedItem as JObject; // 替换为实际数据类型
                  open_card = data["open_card"]["code"].ToString();
                  this.Close();
        }
    }
}
